<?php

/**
 * This is the model class for table "st_user".
 *
 * The followings are the available columns in table 'st_user':
 * @property string $user_id
 * @property string $user_name
 * @property string $user_email
 * @property string $user_password
 * @property integer $user_status
 * @property integer $user_approved
 * @property string $user_nickname
 * @property string $user_sex
 * @property string $user_address
 * @property string $user_tel
 * @property string $user_data_added
 * @property string $user_data_modified
 */
class User extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{user}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_name, user_email, user_password, user_status, user_approved, user_data_added', 'required'),
			array('user_status, user_approved', 'numerical', 'integerOnly'=>true),
			array('user_name, user_email, user_password, user_nickname, user_sex, user_address, user_tel, user_data_added, user_data_modified', 'length', 'max'=>128),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('user_id, user_name, user_email, user_password, user_status, user_approved, user_nickname, user_sex, user_address, user_tel, user_data_added, user_data_modified', 'safe', 'on'=>'search'),
		);
	}

    protected function beforeSave()
    {
        if ($this->isNewRecord) {
            $this->user_data_added = time();
        } else{
            $this->user_data_modified = time();
        }
        $this->user_password = $this->hashPassword($this->user_password);
        $this->user_status = 0;
        $this->user_approved = 0;
        return true;
    }

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('user_id',$this->user_id,true);
		$criteria->compare('user_name',$this->user_name,true);
		$criteria->compare('user_email',$this->user_email,true);
		$criteria->compare('user_status',$this->user_status);
		$criteria->compare('user_approved',$this->user_approved);
		$criteria->compare('user_nickname',$this->user_nickname,true);
		$criteria->compare('user_sex',$this->user_sex,true);
		$criteria->compare('user_address',$this->user_address,true);
		$criteria->compare('user_data_added',$this->user_data_added,true);
		$criteria->compare('user_data_modified',$this->user_data_modified,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
    public function validatePassword($password)
    {
        return CPasswordHelper::verifyPassword($password, $this->user_password);
    }

    public function hashPassword($password)
    {
        return CPasswordHelper::hashPassword($password);
    }
	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
